package cn.hd01.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.hd01.repository.ServantRepository;
import cn.hd01.repository.entity.Servant;
import cn.hd01.service.ServantService;

@Service
public class ServantServiceImpl extends BaseServiceImpl<Servant, Integer> implements ServantService {

	private ServantRepository repository;
	
	@Autowired
	public ServantServiceImpl(ServantRepository repository) {
		super(repository);
		this.repository = repository;
	}

	@Override
	@Cacheable(value="servant", key="#type + 'findByType'")
	public List<Servant> getServant(String type) {
		
		Servant servant = new Servant();
		servant.setType(type);
		
		ExampleMatcher servantMmatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.EXACT);
                
		Example<Servant> e = Example.of(servant, servantMmatcher);
		List<Servant> list = repository.findAll(e);
		
		return list;
	}

	@Override
	@CacheEvict(value="servant", key="#servant.getType() + 'findByType'")
	public int updateServant(Servant servant) {
		int count = repository.updateServant(servant.getName(), servant.getPhoneNo(), servant.getMail(), servant.getAddress(), servant.getId());
		return count;
	}
	
	@Override
	@CacheEvict(value="servant", key="#servant.getType() + 'findByType'")
	public Servant save(Servant servant) {
		servant = repository.save(servant);
		return servant;
	}

	@Override
	public Page<Servant> findAll(final Servant servant, Pageable pageable) {
		if (servant == null) {
			return repository.findAll(pageable);
		}

		return repository.findAll(Specifications.where(new Specification<Servant>() {
			@Override
			public Predicate toPredicate(Root<Servant> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();

				if (StringUtils.hasText(servant.getName())) {
					predicates.add(cb.like(r.get("name").as(String.class), "%" + servant.getName() + "%"));
				}

				if (StringUtils.hasText(servant.getPhoneNo())) {
					predicates.add(cb.like(r.get("phoneNo").as(String.class), "%" + servant.getPhoneNo() + "%"));
				}
				
				if (StringUtils.hasText(servant.getType())) {
					predicates.add(cb.equal(r.get("type").as(String.class), servant.getType()));
				}
				
				return cb.and(predicates.toArray(new Predicate[] {}));
			}
		}), pageable);
		
	}
	
}
